(function (vc) {

    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addPartnerPoolInfo: {
                ppId: '',
                partnerName: '',
                partnerType: '',
                state: 'Y',
                remark: '',
                partnerTypes: [],
                partnerKeys: [],
            }
        },
        _initMethod: function () {

            // vc.getDict('partner_key', 'partner_type', function (_data) {
            //     $that.addPartnerPoolInfo.partnerTypes = _data;
            // })

           
        },
        _initEvent: function () {
            vc.on('addPartnerPool', 'openAddPartnerPoolModal', function () {
                $that._loadAddPartnerAdapt();
                $('#addPartnerPoolModel').modal('show');
            });
        },
        methods: {
            addPartnerPoolValidate() {
                return vc.validate.validate({
                    addPartnerPoolInfo: $that.addPartnerPoolInfo
                }, {
                    'addPartnerPoolInfo.partnerName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "名称不能超过64"
                        },
                    ],
                    'addPartnerPoolInfo.partnerType': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "支付厂家不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "支付厂家不能超过64"
                        },
                    ],
                    'addPartnerPoolInfo.remark': [
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "使用说明不能超过512"
                        },
                    ],
                });
            },
            savePartnerPoolInfo: function () {
                if (!$that.addPartnerPoolValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }

                $that.addPartnerPoolInfo.shopId = vc.getCurrentCommunity().shopId;

                vc.http.apiPost(
                    '/partner.savePartnerPool',
                    JSON.stringify($that.addPartnerPoolInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addPartnerPoolModel').modal('hide');
                            $that.clearAddPartnerPoolInfo();
                            vc.emit('partnerPool', 'listPartnerPool', {});
                            return;
                        }
                        vc.toast(_json.msg);

                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearAddPartnerPoolInfo: function () {
                let _partnerTypes = $that.addPartnerPoolInfo.partnerTypes;
                $that.addPartnerPoolInfo = {
                    partnerName: '',
                    partnerType: '',
                    partnerTypes: _partnerTypes,
                    partnerKeys: [],
                    state: 'Y',
                    remark: '',
                };

            },
            _addChangeToType: function () {
                if (!$that.addPartnerPoolInfo.partnerType) {
                    return;
                }
                let _param = {
                    params: {
                        partnerType: $that.addPartnerPoolInfo.partnerType,
                        page: 1,
                        
                        row: 100
                    }
                }
                //发送get请求
                vc.http.apiGet('/partner.listPartnerKey',
                    _param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        _json.data.forEach(item => {
                            item.columnValue = '';
                        })
                        $that.addPartnerPoolInfo.partnerKeys = _json.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },

            _loadAddPartnerAdapt:function(){
                let param = {
                    params: {
                        page: 1,
                        row: 100,
                    }
                };
                //发送get请求
                vc.http.apiGet('/partner.listPartnerAdapt', param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.addPartnerPoolInfo.partnerTypes = _json.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            }

        }
    });

})(window.vc);
